﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuDao.Api
{
    /// <summary>
    /// 
    /// </summary>
    public static class HttpContextExtention
    {
        /// <summary>
        /// 获取请求客户端的IP地址
        /// </summary>
        public static string GetRequestIP(this HttpContext context)
        {
            var ip = string.Empty;
            var host = context.Request.Host;

            if (host.HasValue)
            {
                ip = host.Host;
                if(ip == "localhost")
                {
                    ip = "127.0.0.1";
                }
            }

            return ip;
        }

        /// <summary>
        /// 获取请求URL地址，格式：httt(s)://host:port/query
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static string GetRequestUrl(this HttpContext context)
        {
            var url = string.Empty;
            //context.Request.p
            return url;
        }

        /// <summary>
        /// 获取请求内容数据，不能直接用Request.Body方式读取
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static string GetRequestBody(this HttpContext context)
        {
            var request = context.Request;

            request.EnableBuffering();
            request.Body.Seek(0, SeekOrigin.Begin);

            using var buffer = new MemoryStream();
            request.Body.CopyToAsync(buffer);// Copy the request stream to the memory stream.

            buffer.Position = 0L;// Rewind the memory stream.
            using var reader = new StreamReader(buffer, Encoding.UTF8);
            var body = reader.ReadToEnd();
            return body;
        }

        ///// <summary>
        ///// Peek at the Http request stream without consuming it
        ///// </summary>
        ///// <param name="request">Http Request object</param>
        ///// <returns>string representation of the request body</returns>
        //public static string PeekBody(this HttpRequest request)
        //{
        //    try
        //    {
        //        request.EnableBuffering();
        //        var buffer = new byte[Convert.ToInt32(request.ContentLength)];
        //        request.Body.Read(buffer, 0, buffer.Length);
        //        return Encoding.UTF8.GetString(buffer);
        //    }
        //    finally
        //    {
        //        request.Body.Position = 0;
        //    }
        //}

        ///// <summary>
        ///// Peek at the Http request stream without consuming it
        ///// </summary>
        ///// <param name="request">Http Request object</param>
        ///// <returns>T type which provided at invocation</returns>
        //public static T PeekBody<T>(this HttpRequest request) where T : class
        //{
        //    try
        //    {
        //        request.EnableBuffering();
        //        var buffer = new byte[Convert.ToInt32(request.ContentLength)];
        //        request.Body.Read(buffer, 0, buffer.Length);
        //        var bodyAsText = Encoding.UTF8.GetString(buffer);
        //        return JsonConvert.DeserializeObject<T>(bodyAsText);
        //    }
        //    finally
        //    {
        //        request.Body.Position = 0;
        //    }
        //}

        ///// <summary>
        ///// Peek asynchronously at the Http request stream without consuming it
        ///// </summary>
        ///// <param name="request">Http Request object</param>
        ///// <returns>T type which provided at invocation</returns>
        //public static async Task<T> PeekBodyAsync<T>(this HttpRequest request) where T : class
        //{
        //    try
        //    {
        //        request.EnableBuffering();
        //        var buffer = new byte[Convert.ToInt32(request.ContentLength)];
        //        await request.Body.ReadAsync(buffer, 0, buffer.Length);
        //        var bodyAsText = Encoding.UTF8.GetString(buffer);
        //        return JsonConvert.DeserializeObject<T>(bodyAsText);
        //    }
        //    finally
        //    {
        //        request.Body.Position = 0;
        //    }
        //}

    }
}
